import { defineStore } from 'pinia'
import { ref } from 'vue'
import { 
  medicalApi, 
  appointmentsApi, 
  servicesApi,
  chartApi
} from '@/api'
import type { 
  Medical, 
  Appointments, 
  ServiceDistribution 
} from '@/types'
import type { StatData, ServiceTypeData, QuickAction, TimeRange, AppointmentChartData } from '@/types/home'

export const useHomeStore = defineStore('home', () => {
  // state
  const recentMedical = ref<Medical[]>([])
  const recentAppointments = ref<Appointments[]>([])
  const serviceDistribution = ref<ServiceDistribution[]>([])
  const appointmentChartData = ref<AppointmentChartData[]>([])

  // 统计数据
  const statsData = ref<StatData[]>([
    {
      title: '今日预约',
      number: 25,
      description: '较昨日 +5',
      trend: 'trend-up'
    },
    {
      title: '在诊宠物',
      number: 8,
      description: '正常水平',
      trend: 'trend-stable'
    },
    {
      title: '待处理',
      number: 3,
      description: '需要关注',
      trend: 'trend-down'
    },
    {
      title: '今日收入',
      number: '￥5,280',
      description: '较昨日 +12%',
      trend: 'trend-up'
    }
  ])

  // 获取图表数据
  const getChartData = (timeRange: TimeRange): number[] => {
    // 只支持week和month
    return appointmentChartData.value.map(item => item.count)
  }

  // 服务类型数据
  const serviceTypeData = ref<ServiceTypeData[]>([
    { value: 335, name: '疫苗接种' },
    { value: 310, name: '健康检查' },
    { value: 234, name: '美容服务' },
    { value: 135, name: '手术治疗' },
    { value: 148, name: '其他服务' }
  ])

  // 快捷操作
  const quickActions = ref<QuickAction[]>([
    { title: '新增预约', route: '/appointments/add' },
    { title: '宠物登记', route: '/pets/add' },
    { title: '就诊记录', route: '/medical/add' },
    { title: '疫苗接种', route: '/vaccine/add' }
  ])

  // actions
  const fetchDashboardData = async () => {
    try {
      // 获取最近医疗记录
      const medicalRes = await medicalApi.getPage({ current: 1, size: 5 })
      console.log('医疗记录响应:', medicalRes)
      recentMedical.value = medicalRes.records || []

      // 获取最近预约
      const appointmentsRes = await appointmentsApi.getPage({ current: 1, size: 5 })
      console.log('预约记录响应:', appointmentsRes)
      
      // 检查不同的数据结构
      if (appointmentsRes.records) {
        recentAppointments.value = appointmentsRes.records
      } else if (appointmentsRes.data && appointmentsRes.data.records) {
        recentAppointments.value = appointmentsRes.data.records
      } else if (Array.isArray(appointmentsRes)) {
        recentAppointments.value = appointmentsRes
      } else {
        console.warn('预约数据格式未知:', appointmentsRes)
        recentAppointments.value = []
      }

    } catch (error) {
      console.error('获取首页数据失败:', error)
      // 设置默认空数组
      recentMedical.value = []
      recentAppointments.value = []
    }
  }

  const fetchAppointmentChartData = async (range: TimeRange) => {
    try {
      const res = await chartApi.getAppointmentChartData(range)
      if (res && Array.isArray(res.data)) {
        appointmentChartData.value = res.data
      } else {
        appointmentChartData.value = []
      }
    } catch (e) {
      appointmentChartData.value = []
    }
  }

  return {
    recentMedical,
    recentAppointments,
    serviceDistribution,
    quickActions,
    statsData,
    getChartData,
    serviceTypeData,
    fetchDashboardData,
    appointmentChartData,
    fetchAppointmentChartData
  }
}) 